package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class TodayBestService {
    @Autowired
    private UserService userService;
    @Autowired
    private RecommendUserService recommendUserService;
    @Value("${tanhua.sso.default.user}")
    private Long defaultUser;

    @Autowired
    private UserInfoService userInfoService;
    public TodayBest queryTodayBest() {
        //获取User对象，无需对User对象校验，其一定不为null
        User user = UserThreadLocal.get();
        //查询推推荐（今日佳人）
        TodayBest todayBest = recommendUserService.queryTodayBest(user.getId());
        if(todayBest == null){
            //给出默认用户
            todayBest = new TodayBest();
            todayBest.setId(defaultUser);
        }
        UserInfo userInfo = userInfoService.queryUserInfoByUserId(todayBest.getId());
        if(null == userInfo){
            return null;
        }
        setTodayBest(todayBest, userInfo);

        return todayBest;
    }

    private void setTodayBest(TodayBest todayBest, UserInfo userInfo) {
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setTags(StringUtils.split(userInfo.getTags(), ','));
        todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
        todayBest.setAge(userInfo.getAge());
    }

    public PageResult queryRecommendation(RecommendUserQueryParam queryParam) {
        //获取User对象，无需对User对象校验，其一定不为null
        User user = UserThreadLocal.get();
        PageResult pageResult = new PageResult();
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPagesize());
        PageInfo<RecommendUser> pageInfo = recommendUserService.queryRecommendation(user.getId(), queryParam.getPage(), queryParam.getPagesize());
        List<RecommendUser> records = pageInfo.getRecords();
        if(CollectionUtils.isEmpty(records)) {
            //没有查询到用户列表
            return pageResult;
        }
        Set<Long> userIds = new HashSet<>();
        //收集用户id
        for (RecommendUser record : records) {
            userIds.add(record.getUserId());
        }
        //根据筛选条件 查询条件构造
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("user_id",userIds);
        if (StringUtils.isNotEmpty(queryParam.getGender())) {
            //需要性别参数查询
           // queryWrapper.eq("sex", StringUtils.equals(queryParam.getGender(), "man") ? 1 : 2);
        }
        if (StringUtils.isNotEmpty(queryParam.getCity())) {
            //需要城市参数查询
           // queryWrapper.like("city", queryParam.getCity());
        }

        if (queryParam.getAge() != null) {
            //设置年龄参数，条件：小于等于
           // queryWrapper.le("age", queryParam.getAge());
        }
        List<UserInfo> userInfoList = userInfoService.queryUserInfoList(queryWrapper);
        if(CollectionUtils.isEmpty(userInfoList)){
            //没有查询到用户的基本信息
            return pageResult;
        }
        List<TodayBest> todayBests = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            TodayBest todayBest = new TodayBest();

            todayBest.setId(userInfo.getUserId());
            setTodayBest(todayBest, userInfo);
            //缘分值
            for (RecommendUser record : records) {
                if(record.getUserId().longValue() == userInfo.getUserId().longValue()){
                    double score = Math.floor(record.getScore());//取整,98.2 -> 98
                    todayBest.setFateValue(Double.valueOf(score).longValue());
                    break;
                }
            }
            todayBests.add(todayBest);
        }
        Collections.sort(todayBests,(t1, t2) -> (int)(t2.getFateValue() - t1.getFateValue()));
        pageResult.setItems(todayBests);
        return pageResult;
    }
}
